package com.ltg.urban.domain.project.controller;

import com.ltg.framework.annotation.CurrentUser;
import com.ltg.framework.annotation.DescribePage4Swagger;
import com.ltg.framework.util.http.PageInfo;
import com.ltg.framework.util.http.PageRequest;
import com.ltg.framework.util.http.Result;
import com.ltg.urban.domain.project.data.request.*;
import com.ltg.urban.domain.project.data.response.ProjectDetailResp;
import com.ltg.urban.domain.project.data.response.ProjectPageResp;
import com.ltg.urban.domain.project.entity.UrbanProject;
import com.ltg.urban.domain.project.entity.UrbanProjectUser;
import com.ltg.urban.domain.project.service.UrbanProjectService;
import com.ltg.urban.domain.sys.data.response.UserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p> ClassName: UrbanProjectController </p>
 * <p> Package: com.ltg.urban.domain.project.entity.controller </p>
 * <p> Description: </p>
 * <p></p>
 *
 * @Author: LTG
 * @Create: 2023/2/22 - 21:47
 * @Version: v1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/urban/project")
@Tag(name = "项目模块")
public class UrbanProjectController {

    /**
     * 6.项目模块:主要实现对项目信息的管理和维护。用户可以在平台上查询项目信息。项目管理员可以管理项目对应的项目经理和普通用户。项目经理和普通员工在系统上提交项目立项到结束过程中具体的项目文档信息和执行过程信息。
     */

    /**
     * 7.资金模块:是要实现项目资金信息的维护和管理。例如:项目经理查看资金使用情况，普通员工新增资金使用情
     * 况
     * 8.首页大屏模块:在平台首页展示系统统计信息，例如项目数量 项目完成情况 项目资金使用情况等相关业务信息或
     * 统计图信息。
     * 9.审批模块:项目在申报审批执行结束的整个过程中通过系统审批完成项目的立项到结果的过程。
     */
    /**
     * 项目申报
     */
    private final UrbanProjectService urbanProjectService;

    @Operation(summary = "项目分页查询:OK")
    @GetMapping("/query")
    @Parameter(name = "status", in = ParameterIn.QUERY, description = "0立项、1送审、2审批完成、3项目进行、4项目验收、5项目完结", schema = @Schema(type = "integer"))
    @Parameter(name = "approveResult", in = ParameterIn.QUERY, description = "审批结果:0审批中、1审批成功、-1审批失败:审批失败之后重申,status变为1", schema = @Schema(type = "integer"))
    @Parameter(name = "projectName", in = ParameterIn.QUERY, description = "搜索", schema = @Schema(type = "string"))
    @DescribePage4Swagger
    public PageInfo<ProjectPageResp> query(HttpServletRequest httpServletRequest,
                                           @CurrentUser UserInfo userInfo,
                                           @RequestParam(required = false) Integer status,
                                           @RequestParam(required = false) Integer approveResult,
                                           @RequestParam(required = false)String projectName) {
        return urbanProjectService.pageList(userInfo, PageRequest.buildFromRequest(httpServletRequest), status, approveResult,projectName);

    }

    @PostMapping("/apply")
    @Operation(summary = "项目申报:OK")
    public Result<UrbanProject> apply(@RequestBody ApplyProjectReq req) {
        return urbanProjectService.apply(req);
    }

    @GetMapping("/{projectId}/detail")
    @Operation(summary = "项目详情:OK")
    public Result<ProjectDetailResp> detail(@PathVariable Long projectId) {
        return urbanProjectService.projectDetail(projectId);
    }

    /**
     * 开工按钮
     *
     * @param projectId
     * @return
     */
    @PutMapping("/{projectId}/changeStatus")
    @Operation(summary = "开工、完工:项目经理更改状态:OK")
    public Result<UrbanProject> projectChangeStatus(@PathVariable Long projectId,
                                                    @RequestBody ModifyProjectStatusReq req) {
        return urbanProjectService.projectChangeStatus(projectId, req);
    }

    @PutMapping("/{projectId}/approve")
    @Operation(summary = "项目审批:OK")
    public Result<UrbanProject> projectApprove(@PathVariable Long projectId, @RequestBody ApproveProjectReq req) {
        return urbanProjectService.projectApprove(projectId, req);
    }

    /**
     * 项目更新成员
     *
     * @param projectId
     * @param req
     * @return
     */
    @PutMapping("/{projectId}/create/user")
    @Operation(summary = "添加项目成员:OK")
    public Result<UrbanProjectUser> addProjectUser(@PathVariable Long projectId, @RequestBody ModifyUserReq req) {
        return urbanProjectService.addProjectUser(projectId, req.getMember());
    }

    @PutMapping("/{projectId}/delete/{userId}")
    @Operation(summary = "删除项目成员:OK")
    public Result<Void> deleteProjectUser(@PathVariable Long projectId, @PathVariable Long userId) {
        return urbanProjectService.deleteProjectUser(projectId, userId);
    }

    @PutMapping("/{projectId}/modify")
    @Operation(summary = "项目修改:当project.status===0||(project.status===2&&project.approveResult===-1):OK")
    public Result<UrbanProject> modify(@PathVariable Long projectId, @RequestBody ApplyProjectReq req) {
        return urbanProjectService.modify(projectId, req);
    }


    @PutMapping("/{projectId}/anewApply")
    @Operation(summary = "重新申请")
    public Result<UrbanProject> anewApply(@PathVariable Long projectId, AnewApplyReq req) {
        return urbanProjectService.anewApply(projectId, req);
    }


}
